<?php

class Feeding_model extends Model {


    function Feeding_model() {
        // Call the Model constructor
        parent::Model();
        
        // load DB
        $this->load->database();
    }
    
    /*
        GET FEEDS
    */
    function get_feeds() {
        // returns all feeds
        $query = $this->db->get('feeds');
        return $query->result();
    }
    
    function get_feed($id) {
        // returns a specific feed
        if ($id) {
            $this->db->select('*');
            $this->db->from('feeds');
            $this->db->where('id', $id);

            $query = $this->db->get();
            return $query->result();
        }
    }
    
    function get_feed_tags() {
        // returns all feed_tags
        $query = $this->db->get('feed_tags');
        return $query->result();
    }
    
    function get_feed_tag($id) {
        // returns a specific feed_tag
        if ($id) {
            $this->db->select('*');
            $this->db->from('feed_tags');
            $this->db->where('id', $id);

            $query = $this->db->get();
            return $query->result();
        }
    }
    
    /*
        ADD/EDIT FEEDS
    */
    function add_feed($uri) {
        if ($uri) {
            $data = array(
               'uri' => $uri
            );
            
            $this->db->insert('feeds', $data);   
        }
    }

    function edit_feed($id, $uri) {
        if ($id && $uri) {
            $data = array(
                'uri' => $uri
            );

            $this->db->where('id', $id);
            $this->db->update('feeds', $data);
        }
    }
    
    function add_feed_tag($feed_id, $root_tag_id, $tag_id) {
        if ($feed_id && $tag_id) {
            $data = array(
               'feed_id' => $feed_id,
               'root_tag_id' => $root_tag_id,
               'tag_id' => $tag_id
            );
            
            $this->db->insert('feed_tags', $data);   
        }
    }
    
    function edit_feed_tag($id, $tag_id, $root_tag_id) {
        if ($id && $tag_id) {
            $data = array(
                'tag_id' => $tag_id,
                'root_tag_id' => $root_tag_id
            );

            $this->db->where('id', $id);
            $this->db->update('feed_tags', $data);
        }       
    }
    
}

?>